package com.tuzhi.testjpa.web;

import com.tuzhi.base.jpa.repository.CommonSqlRepo;
import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.testjpa.TestJpaEmImpl;
import com.tuzhi.testjpa.jpa1.SysUserEntity;
import com.tuzhi.testjpa.jpa1.SysUserRepo;
import com.tuzhi.testjpa.jpa2.SysUserRepo1;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/test/commonrepo")
public class TestCommonRepoController {

    @Autowired
    CommonSqlRepo commonRepo;

    @Autowired
    TestJpaEmImpl testJpaEm;

    @Autowired
    SysUserRepo sysUserRepo;

    @Autowired
    SysUserRepo1 sysUserRepo1;

    @GetMapping(value = { "/findSingleResultBySql/{id}"})
    public AjaxResultVO findSingleResultBySql(@PathVariable Long id) {
        String sql = "select username from sys_user where id=?1";
        String name = null;
        try {
            name = (String) commonRepo.findSingleResultBySql(sql,  id);
            System.out.println(name);

            List<SysUserEntity> tmpList = commonRepo.findAllByJpql("select u from SysUserEntity u where id=?1", SysUserEntity.class, id);
            System.out.println(tmpList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
//            String sql2 = "select log_type from sys_log where id=?1";
            name = (String) testJpaEm.findSingleResultBySql(sql,  id);
            System.out.println(name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List list = null;
        try {
            list = (List) sysUserRepo.findAll(new ArrayList<>());
            System.out.println(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            List tmp = sysUserRepo1.findAll(new ArrayList<>());
            System.out.println(tmp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        String tmpName = RandomUtils.nextInt() + "";
        commonRepo.executeSql("update sys_user set username='" + tmpName + "' where id=5");

        return AjaxResultHelper.success().ofData(name);
    }

}
